package com.strato.rpc.api.v1_0_0;

import java.util.List;

import org.osgi.framework.BundleContext;

import com.strato.base.env.v1_0_0.GlobalEnvironment;
import com.strato.base.osgi.v1_0_0.BundleUtil;
import com.strato.rpc.api.v1_0_0.activator.Activator;

public interface Registry {

	public static final String AUTO_DETECT="(AUTO)";
	
	Object createProxy(String id, Class<?> intf,int retry);

	void register(String id, String transport, Object object,Class<?> intf, String bundleName);

	void unregister(String id);
	
	/**
	 * if AUTO_DETECT,try ${strato.rpc.registry} from GlobalEnvironment,then get default from bundleContext
	 * @param id
	 * @return
	 */
	public static Registry get(String id){
		BundleContext context = Activator.getContext();
		if(AUTO_DETECT.equals(id)){
			String configId=GlobalEnvironment.getInstance().get("strato.rpc.registry");
			if(configId!=null){
				return BundleUtil.getService(context, Registry.class, "(id="+configId+")");
			}
			return BundleUtil.getService(context, Registry.class,null);
		}
		return BundleUtil.getService(context, Registry.class, "(id="+id+")");
	}

	public static List<Registry> list(){
		BundleContext context = Activator.getContext();
		return BundleUtil.getServices(context, Registry.class, null);
	}
}
